*! version 2.0
* 21 August 2013
* NIDS 
* Merging Individual questionnaires to the Household roster.

* Will run on all waves

*===========================================================================================================================================
* GLOBALS FOR DATA FILES AND VERSION SUFFIXES

global DataIN "\\137.158.104.21\data\Panel Public Release 2014a\Wave 3\Anon"
global DataOUT "C:\Users\01406074\Desktop"
global VersionIN "W3_Anon_V1.2"
global VersionOUT "merged"

global temp "C:\Users\01406074\Desktop"			// tempfile to hold all the working datasets, all working datasets will
																// be deleted from this folder at the completion of the do file.
																		
version 12.0													// version of Stata being used, this is needed for the rename command.

*===========================================================================================================================================

* OPENING THE INDIVIDUAL FILES, REMOVING INDIVIDUAL SPECIFIC PREFIX, REPLACING WITH WAVE SPECIFIC PREFIX AND APPENDING INDIVIDUAL DATA TOGETHER

* ADULT

use "$DataIN\Adult_$VersionIN.dta", clear				// opening the dataset
	
rename w#_a* w#*										// replacing the current prefix with a wave specific prefix
gen dataset = "Adult"									// generating a variable to indicate the source dataset

save "$temp\adult.dta", replace							// saving the modified data as a temp file
	
* PROXY

use "$DataIN\Proxy_$VersionIN.dta", clear				// opening the dataset

rename w#_p* w#*										// replacing the current prefix with a wave specific prefix
gen dataset = "Proxy"									// generating a variable to indicate the source dataset

save "$temp\proxy.dta", replace							// saving the modified data as a temp file

* CHILD

use "$DataIN\Child_$VersionIN.dta", clear				// opening the dataset

rename w#_c* w#*										// replacing the current prefix with a wave specific prefix
gen dataset = "Child"									// generating a variable to indicate the source dataset

append using "$temp\adult.dta"							// appending the modified adult temp file to the adjusted child file
append using "$temp\proxy.dta"							// appending the modified proxy temp file to the adjusted child file

order dataset, last										// moving the variable "dataset" to the end of the dataset

save "$temp\indi.dta", replace							// saving the modified data as a temp file

*-------------------------------------------------------------------------------------------------------------------------------------------

* OPENING THE HOUSEHOLD ROSTER AND MERGING THE INDIVIDUAL FILE CREATED ABOVE INTO IT.

use "$DataIN\HouseholdRoster_$VersionIN.dta", clear		// opening the household roster

merge 1:1 pid *hhid using "$temp\indi.dta"				// performing a 1 to 1 merge on pid and hhid, between the individuals and the roster 

save "$DataOUT\Roster_Indi_$VersionOUT.dta", replace	// saving out the resulting dataset, the defined path, as per globals

*-------------------------------------------------------------------------------------------------------------------------------------------

* ERASING THE TEMP FILES

erase "$temp\adult.dta"
erase "$temp\proxy.dta"
erase "$temp\indi.dta"

*===========================================================================================================================================

********** NOTE TO USER ************ 

*===========================================================================================================================================
/*

The _merge variable that is generated as a result of the merge, has the following output:

_merge == 3  (matched)			// matched indicating that individuals were merged to the roster perfectly	
_merge == 2  (from using)   	// indicates that there are no individuals from the generated indi temp file, that are not being merged into the roster							

Finally and most important to highlight

_merge == 1  (from master)		// represent individuals that are listed on the roster but are non-residents or deceased individuals and thus have no individual level questionnaires.

***************************************************************************

WAVE 1 _merge results

Result                           # of obs.
-----------------------------------------
not matched                         2,918
from master                         2,918	(_merge==1)
from using                             0	(_merge==2)

matched                            28,226	(_merge==3)
-----------------------------------------

***************************************************************************

WAVE 2 _merge results

Result                           # of obs.
-----------------------------------------
not matched                         2,083
from master                         2,083	(_merge==1)
from using                              0	(_merge==2)

matched                            34,098	(_merge==3)
-----------------------------------------

*************************************************************************** 

WAVE 3 _merge results

Result                           # of obs.
-----------------------------------------
not matched                         4,794
from master                         4,794	(_merge==1)
from using                              0	(_merge==2)

matched                            37,436	(_merge==3)
-----------------------------------------

*/

*end of do file
*--------------------------------------------------------------------------------------------------------------------------------------------



